﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ListCustomerNameTracking : Form
    {
        private int customerID;
        private int serialID;

        public ListCustomerNameTracking(int customerID, string descriptionOfficial)
        {
            InitializeComponent();

            this.customerID = customerID;
            this.Text = this.Text + " [" + descriptionOfficial + "]";

            StackedHeaderDecorator stackedHeaderDecoratorAvailable = new StackedHeaderDecorator(this.dataGridCustomerNameTracking);

            DataTable dataTableTracking = SQLDatabase.GetDataTable(" SELECT     ListCustomerNameTracking.SerialID, ListImportBatchMaster.ImportDate, ListDistributorName.DescriptionOfficial AS DistributorName, ListCustomerType.Description AS CustomerTypeName, ListCustomerChannel.Description AS CustomerChannelName, ListCustomerNameTracking.DescriptionOfficial, ListCustomerNameTracking.VATCode, ListCustomerNameTracking.ContactName, ListCustomerNameTracking.AttentionName, ListCustomerNameTracking.Telephone, ListCustomerNameTracking.EMailAddress, ListCustomerNameTracking.AddressNo, ListAddressArea.Description AS AddressAreaName " +
                                                                   " FROM       ListCustomerNameTracking INNER JOIN ListDistributorName ON ListCustomerNameTracking.DistributorID = ListDistributorName.DistributorID INNER JOIN ListImportBatchMaster ON ListCustomerNameTracking.ImportBatchID = ListImportBatchMaster.ImportBatchID LEFT JOIN ListCustomerType ON ListCustomerNameTracking.CustomerTypeID = ListCustomerType.CustomerTypeID LEFT JOIN " +
                                                                   "            ListCustomerChannel ON ListCustomerNameTracking.CustomerChannelID = ListCustomerChannel.CustomerChannelID LEFT JOIN " +
                                                                   "            ListAddressArea ON ListCustomerNameTracking.AddressAreaID = ListAddressArea.AddressAreaID " +
                                                                   " WHERE      ListCustomerNameTracking.CustomerID = " + this.customerID);

            this.dataGridCustomerNameTracking.DataSource = dataTableTracking;
        }

        private void dataGridCustomerNameTracking_SelectionChanged(object sender, EventArgs e)
        {
            DataRow dataRowTracking = ((DataRowView)this.dataGridCustomerNameTracking.CurrentRow.DataBoundItem).Row;
            if (dataRowTracking != null && dataRowTracking["SerialID"] != DBNull.Value)
            {
                this.serialID = (int)dataRowTracking["SerialID"];
                this.toolStripButtonShowEventLog.Enabled = true;
                this.toolStripButtonApplyToOwner.Enabled = true;
            }
            else
            {
                this.serialID = 0;
                this.toolStripButtonShowEventLog.Enabled = false;
                this.toolStripButtonApplyToOwner.Enabled = false; 
            }
        }

        private void toolStripButtonEscapeAndApplyToOwner_Click(object sender, EventArgs e)
        {
            try
            {
                if (sender.Equals(this.toolStripButtonApplyToOwner))
                {
                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to apply this change to the owner?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
                    if (dialogResult == DialogResult.Yes)
                    {
                        DataTable dataTableTracking = SQLDatabase.GetDataTable("SELECT * FROM ListCustomerNameTracking WHERE SerialID = " + this.serialID);
                        if (dataTableTracking.Rows.Count > 0)
                        {
                            if ((int)dataTableTracking.Rows[0]["CustomerTypeID"] != 0) SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET CustomerTypeID = " + dataTableTracking.Rows[0]["CustomerTypeID"] + " WHERE CustomerID = " + this.customerID);
                            if ((int)dataTableTracking.Rows[0]["CustomerChannelID"] != 0) SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET CustomerChannelID = " + dataTableTracking.Rows[0]["CustomerChannelID"] + " WHERE CustomerID = " + this.customerID);
                            if ((int)dataTableTracking.Rows[0]["AddressAreaID"] != 0) SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET AddressAreaID = " + dataTableTracking.Rows[0]["AddressAreaID"] + " WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["DescriptionOfficial"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET DescriptionOfficial = N'" + dataTableTracking.Rows[0]["DescriptionOfficial"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["VATCode"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET VATCode = N'" + dataTableTracking.Rows[0]["VATCode"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["ContactName"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET ContactName = N'" + dataTableTracking.Rows[0]["ContactName"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["AttentionName"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET AttentionName = N'" + dataTableTracking.Rows[0]["AttentionName"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["Telephone"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET Telephone = N'" + dataTableTracking.Rows[0]["Telephone"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["EMailAddress"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET EMailAddress = N'" + dataTableTracking.Rows[0]["EMailAddress"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["AddressNo"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET AddressNo = N'" + dataTableTracking.Rows[0]["AddressNo"] + "' WHERE CustomerID = " + this.customerID);
                            if ((string)dataTableTracking.Rows[0]["AddressStreet"] != "") SQLDatabase.ExecuteNonQuery("UPDATE ListCustomerName SET AddressStreet = N'" + dataTableTracking.Rows[0]["AddressStreet"] + "' WHERE CustomerID = " + this.customerID);
                        }
                        MessageBox.Show(this, "Congratulation! Apply Susscessfully!", "Apply tracking", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                    }
                }

                if (sender.Equals(this.toolStripButtonEscape))
                    this.DialogResult = DialogResult.Cancel;
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        
        private void toolStripButtonShowEventLog_Click(object sender, EventArgs e)
        {
            //Call lShowEventLog(ENMVNTaskID.EListCustomerName, Int(Me.vsFMaster.RowData(Me.vsFMaster.RowSel)))
        }
    }
}
